﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml.Linq;

namespace FortnoxAPI
{
    /// <summary>
    /// Get series from Fortnox
    /// </summary>
    public partial class GetSeries
    {

        /// <summary>
        /// Gets the values.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="token">The token.</param>
        /// <param name="db">The db.</param>
        /// <param name="year">The year.</param>
        /// <returns></returns>
        public static DataTable GetValues(string url, string token, string db, string year)
        {
            XDocument doc = XDocument.Load(url + "token=" + token + "&db=" + db + "&year=" + year);

            DataSet ds = new DataSet();

            DataTable dt1 = new DataTable();
            dt1.Columns.Add("descr", typeof(string));
            dt1.Columns.Add("nextval", typeof(string));
            dt1.Columns.Add("short", typeof(string));

            IEnumerable<XElement> Posts = from post in doc.Descendants("series")

                                          //where (string)post.Attribute("id").Value == "1"
                                          select post;

            foreach (XElement Post in Posts)
            {
                DataRow dr1 = dt1.NewRow();

                if (Post.Attribute("descr") != null)
                {
                    dr1["descr"] = Post.Attribute("descr").Value;
                }
                else
                {
                    dr1["descr"] = "";
                }

                if (Post.Attribute("nextval") != null)
                {
                    dr1["nextval"] = Post.Attribute("nextval").Value;
                }
                else
                {
                    dr1["nextval"] = "";
                }

                if (Post.Attribute("short") != null)
                {
                    dr1["short"] = Post.Attribute("short").Value;
                }
                else
                {
                    dr1["short"] = "";
                }

                dt1.Rows.Add(dr1);
            }
            return dt1;
        }
    }
}
